<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Tags extends AdminController {
	
	var $num_elements = 1;
	
	function Tags() {
		parent::AdminController ();
		
		$this->load->library('Form_validation');		
		$this->load->model ( "Tag_Model", "Tags");
		
		$num_elements = $this->settings->item("_admin_list_elements");
		if($num_elements != NULL){
			$this->num_elements = $num_elements;
		}
		
	}
	
	function index() {
		
		$this->load->library('pagination');
		
		$pag = $this->pagination->segment_value();
		if($pag == NULL){
			$pag = 0;			
		}
		
		$total = $this->Tags->getCount(array("status" => "all", "hide_empty" => FALSE));
		$elements_process = $this->Tags->get(array("status" => "all", "num_nodes" => TRUE, "hide_empty" => FALSE), $pag, $this->num_elements);
		
		$config['base_url'] = "/admin/tags/index/";
		$config['total_rows'] = $total;
		$config['per_page'] = $this->num_elements;
		//$config['uri_segment'] = 4;
		
		$config['full_tag_open'] = '<div class="pagination">';
		$config['full_tag_close'] = '</div>';
		
		$config['num_tag_open'] = '<span class="number">';
		$config['num_tag_close'] = '</span>';
		
		$config['cur_tag_open'] = '<span class="number current">';
		$config['cur_tag_close'] = '</span>';
		
		$config['first_link'] = 'Inicio';
		$config['last_link'] = 'Último';
		$config['prev_link'] = '&lt; Ant';
		$config['next_link'] = 'Sig &gt;';

		$this->pagination->initialize($config);
		
		$this->_addData("tags", $elements_process);
		
		$this->_render();
	}
	
	function add() {
		
		$this->form_validation->set_rules('name', 'Nombre', 'required|max_length[60]');
		$this->form_validation->set_rules('url', 'Url', 'required|max_length[200]');
		$this->form_validation->set_rules('visible', 'Visible', 'required');
		$this->form_validation->set_error_delimiters('', '<br />');

		if ($this->form_validation->run() == TRUE)
		{
			$fields = array(
				'name'		=> $this->input->post('name'),
				'url_name'	=> $this->input->post('url'),
				'visible'	=> $this->input->post('visible'),
			);
			
			$this->Tags->create($fields);
			
			$this->_addFlashSuccess("Etiqueta creada correctamente");
			redirect('admin/tags', 'refresh');
		}
		
		$this->_render();
	}

	function edit($id = NULL) {
		$this->_setPageView("tags/add");
		
		$this->form_validation->set_rules('name', 'Nombre', 'required|max_length[60]');
		$this->form_validation->set_rules('url', 'Url', 'required|max_length[200]');
		$this->form_validation->set_rules('visible', 'Visible', 'required');
		$this->form_validation->set_error_delimiters('', '<br />');

		if ($this->form_validation->run() == TRUE){
			$fields = array(
				'id'		=> $id,
				'name'		=> $this->input->post('name'),
				'url_name'	=> $this->input->post('url'),
				'visible'	=> $this->input->post('visible'),
			);
			
			$this->Tags->update($fields);
			
			$this->_addFlashSuccess("Etiqueta modificada correctamente");
			redirect('admin/tags', 'refresh');
			
		} else{				
		
			if($id != NULL){
				$tag = $this->Tags->get(array("id" => $id, "status" => "all"));
				
				if($tag !== FALSE){
					$this->_addData("tag", $tag);
				}
				
				$this->_addData("id", $id);
			}
		}
		
		$this->_render();
	}
	

	function delete($id) {
		if($id != null){
			$this->Tags->delete($id);
			$this->_addFlashSuccess("Etiqueta eliminada correctamente");
		}
		
		redirect ( 'admin/tags', 'refresh' );
	}

}
?>
